/**
 * Copyright (c) 2003 安徽电信工程有限责任公司
 */
package com.ahccc.codegenerator.controller;

import com.ahccc.codegenerator.Zip;
import com.ahccc.codegenerator.backend.jpa.JpaBackendGenerate;
import com.ahccc.codegenerator.backend.mybatis.MybatisBackendGenerator;
import com.ahccc.codegenerator.config.GenerateConfig;
import com.ahccc.codegenerator.entity.Task;
import com.ahccc.codegenerator.frontend.vue.VueFrontendGenerate;
import com.ahccc.codegenerator.service.ITaskService;
import com.ahccc.codegenerator.vo.DbConnInfo;
import com.ahccc.common.util.SpringUtil;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @author lichunqing
 * @date 2019-03-15
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private ITaskService iTaskService;

    @Resource
    private MybatisBackendGenerator mybatisBackendGenerator;

    @RequestMapping("/{id}")
    public Task get(Long id){
        return iTaskService.getById(id);
    }

    @RequestMapping("/list")
    public List<Task> list(){
        return iTaskService.list();
    }

    @RequestMapping("/save")
    public boolean save(@RequestBody Task task){
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        return iTaskService.save(task);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody Task task){
        task.setUpdateTime(new Date());
        return iTaskService.updateById(task);
    }

    @RequestMapping("/remove")
    public boolean remove(Long id){
        return iTaskService.removeById(id);
    }

    @RequestMapping("/getSchemaInfo")
    public Object getSchemaInfo(Task task) {
        GlobalConfig gc = new GlobalConfig();
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(DbConnInfo.getMySqlUrl(task.getDbHost(), task.getDbPort(), task.getDbSchema()));
        dsc.setDriverName(DbConnInfo.getDriveName(task.getDbType()));
        dsc.setUsername(task.getDbUsername());
        dsc.setPassword(task.getDbPassword());
        StrategyConfig strategy = new StrategyConfig();
        ConfigBuilder configBuilder = new ConfigBuilder(null, dsc, strategy, null, gc);
        return configBuilder.getTableInfoList();
    }

    @RequestMapping("/generate")
    public String generate(Task task,
                          String framework,
                          @RequestParam("selectedTableNames[]") String[] selectedTableNames) throws Exception {
        if("jpa".equals(framework)){
            JpaBackendGenerate jpaBackendGenerate=new JpaBackendGenerate();
            jpaBackendGenerate.generateParent(task);
            jpaBackendGenerate.generateServer(task,selectedTableNames);
            jpaBackendGenerate.generateWeb(task,selectedTableNames);
            VueFrontendGenerate vueFrontendGenerate=new VueFrontendGenerate();
            vueFrontendGenerate.generateWeb(task,selectedTableNames);
            GenerateConfig generateConfig = SpringUtil.getBean(GenerateConfig.class);
            String outDir=generateConfig.getPath() +task.getName();
            String zipFileName = outDir + ".zip";
            Zip.zipDirectory(outDir, zipFileName);
            return zipFileName;
        }
        if("mybatis".equals(framework)){
            return mybatisBackendGenerator.execute(task, selectedTableNames);
        }
        return null;
    }
}
